package com.sjx.lib_weather.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.sjx.lib_base.utils.DateUtils
import com.sjx.lib_base.utils.WeatherUtil
import com.sjx.lib_network.bean.DailyItem
import com.sjx.lib_weather.R

/**
 * FileName: DailyAdapter
 * Founder: sjx
 * Profile: 未来七天天气适配器
 */
class DailyAdapter(private val mDailyList: List<DailyItem>) :
    RecyclerView.Adapter<DailyAdapter.DailyHolder>() {


    inner class DailyHolder(itemView: View) : ViewHolder(itemView) {
        val tvDate: TextView = itemView.findViewById(R.id.tv_date)
        val ivIcon: ImageView = itemView.findViewById(R.id.iv_weather_state)
        val tvHighestTemperature: TextView = itemView.findViewById(R.id.tv_temp_height)
        val tvLowestTemperature: TextView = itemView.findViewById(R.id.tv_temp_low)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        DailyHolder(
            LayoutInflater.from(parent.context)
                .inflate(R.layout.item_weather_forecast_list, parent, false)
        )

    override fun getItemCount() = mDailyList.size

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: DailyHolder, position: Int) {
        val daily = mDailyList[position]
        holder.tvDate.text = DateUtils.dateSplitPlus(daily.fxDate) + DateUtils.Week(daily.fxDate)
        holder.tvHighestTemperature.text = daily.tempMax + "℃"
        holder.tvLowestTemperature.text = daily.tempMin + "℃"
        WeatherUtil.changeIcon(holder.ivIcon, daily.iconDay.toInt())
    }


}

